//
//  UUTableView.swift
//  quanyihui
//
//  Created by Galaxy on 2022/6/29.
//

import UIKit

class UUTableView: UITableView,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate {

    @objc var listArr:[Any] = []{
        didSet{
            reloadData()
        }
    }
    @objc var scrollViewDidScrollBlock: ((_ scrollView: UIScrollView) -> ())?

    private var cellItemDidSelectedBlock:((_ indexPath:IndexPath)->())?
    private var cellForRowBlock:((_ indexPath:IndexPath)-> UITableViewCell)?
    ///设置点击事件
    @objc func cellItemDidSelected(_ closure:@escaping (_ indexPath:IndexPath)->Void){
        cellItemDidSelectedBlock = closure
    }
    ///设置cell样式
    @objc func cellForRow(_ closure:@escaping (_ indexPath:IndexPath)->UITableViewCell){
        cellForRowBlock = closure
    }
    
    override init(frame: CGRect, style: UITableView.Style) {
        super.init(frame: frame, style: style)
        delegate = self
        dataSource = self
        separatorStyle = .none
        estimatedRowHeight = UITableView.automaticDimension
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        listArr.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        cellForRowBlock!(indexPath)
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if cellItemDidSelectedBlock != nil {
            cellItemDidSelectedBlock!(indexPath)
        }
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if scrollViewDidScrollBlock != nil {
            scrollViewDidScrollBlock!(scrollView)
        }
    }
}
